package com.ehotting.eaf.core;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrBuilder;

import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;


/**
 * 
 * String帮助类
 * 
 * @author: 何绍亲
 * @date 创建时间：2011-6-7 下午04:33:56
 * @see {@link org.springframework.util.StringUtils}
 * 
 */
public class StringHelper extends org.springframework.util.StringUtils {

	/**
	 * The empty String <code>""</code>.
	 */
	public static final String EMPTY = "";

	/**
	 * 将首字母大写
	 * 
	 * @param str
	 * @return String 返回首字母大写后的字符串
	 */
	public static String capitalize(String str) {
		if (StringHelper.hasLength(str)) {
			StringBuffer wrod = new StringBuffer();

			str = wrod.append(str.substring(0, 1).toUpperCase()).append(str.substring(1)).toString();
		}
		return str;
	}

	/**
	 * 将首字母小写
	 * 
	 * @param str
	 * @return String 返回首字母小写的字符串
	 */
	public static String firstLetterLowercase(String str) {
		if (StringHelper.hasLength(str)) {
			StringBuffer wrod = new StringBuffer();
			str = wrod.append(str.substring(0, 1).toLowerCase()).append(str.substring(1)).toString();
		}
		return str;
	}

	/**
	 * 
	 * 通过正则表达式检查某个字符串是否满足某种格式
	 * 
	 * @param str
	 *            - 字符串，不能为空或null
	 * @param regex
	 *            - 正则表达式，不能为空或null
	 * @return boolean 满足正则表达式规定格式时返回true
	 * @throw IllegalArgumentException 当str或regex为空时抛出。
	 */
	public static boolean rightFormat(String str, String regex) {
		AssertUtils.notEmpty(str, "The str not should be empty or null!");
		AssertUtils.notEmpty(regex, "The regex not should be empty or null!");

		Pattern pattern = Pattern.compile(regex);
		return pattern.matcher(str).matches();
	}

	/**
	 * @see StringUtils#isBlank(String)
	 */
	public static boolean isBlank(String str) {
		return StringUtils.isBlank(str);
	}
	
	/**
	 * @see StringUtils#isBlank(String)
	 */
	public static boolean arrayContainValue(String[] array,String value) {
		if(array==null || array.length==0 || !StringHelper.hasText(value))
			return false;
		for(String s : array){
			if(value.equals(s)){
				return true;
			}
		}
		return false;
	}

	public static boolean arrayContainValue(List<String> array,String value) {
		if(array==null || array.size()==0 || !StringHelper.hasText(value))
			return false;
		return arrayContainValue(array.toArray(new String[array.size()]),value);
	}


	/**
	 * @see StringUtils#isNotBlank(String)
	 */
	public static boolean isNotBlank(String str) {
		return StringUtils.isNotBlank(str);
	}

	/**
	 * @see StringUtils#trimToEmpty(String)
	 */
	public static String trimToEmpty(String str) {
		return StringUtils.trimToEmpty(str);
	}

	

	/**
	 * @see StringUtils#removeEndIgnoreCase(String, String)
	 * */
	public static String removeEndIgnoreCase(String str, String remove) {
		return StringUtils.removeEndIgnoreCase(str, remove);
	}

	/**
	 * @see StringUtils#removeEnd(String, String)
	 * */
	public static String removeEnd(String str, String remove) {
		return StringUtils.removeEnd(str, remove);
	}

	/**
	 * @see StringUtils#removeStart(String, String)
	 * */
	public static String removeStart(String str, String remove) {
		return StringUtils.removeStart(str, remove);
	}

	/**
	 * @see StringUtils#removeStartIgnoreCase(String, String)
	 * */
	public static String removeStartIgnoreCase(String str, String remove) {
		return StringUtils.removeStartIgnoreCase(str, remove);
	}

	/**
	 * @see StringUtils#remove(String, String)
	 * */
	public static String remove(String str, String remove) {
		return StringUtils.remove(str, remove);
	}

	/**
	 * @see StringUtils#endsWithIgnoreCase(String, String)
	 * */
	public static boolean equalsIgnoreCase(String str1, String str2) {
		return StringUtils.equalsIgnoreCase(str1, str2);
	}

	/**
	 * @see StringUtils#equals(String, String)
	 */
	public static boolean equals(String str1, String str2) {
		return StringUtils.equals(str1, str2);
	}

	/**
	 * @see StringUtils#indexOf(String, String)
	 * */
	public static int indexOf(String str, String searchStr) {
		return StringUtils.indexOf(str, searchStr);
	}

	

	/**
	 * @see StringUtils#defaultString(String)
	 * */
	public static String defaultString(String str) {
		return str == null ? EMPTY : str;
	}

	/**
	 * @see StringUtils#defaultString(String, String)
	 */
	public static String defaultString(String str, String defaultStr) {
		return str == null ? defaultStr : str;
	}

	/**
	 * @see StringUtils#defaultIfBlank(String, String)
	 */
	public static String defaultIfBlank(String str, String defaultStr) {
		return StringUtils.isBlank(str) ? defaultStr : str;
	}

	/**
	 * @see StringUtils#defaultIfEmpty(String, String)
	 */
	public static String defaultIfEmpty(String str, String defaultStr) {
		return StringUtils.isEmpty(str) ? defaultStr : str;
	}

	/**
	 * @see StringUtils#reverse(String)
	 */
	public static String reverse(String str) {
		if (str == null) {
			return null;
		}
		return new StrBuilder(str).reverse().toString();
	}
	
	/**
	 * 随机产生编码
	 * @param prefix 字符串前缀,可以为空
	 * @param numLen 数字的长度
	 * @return 字符串前缀+numLen位随机数字
	 */
	public static String randomCode(String prefix,int numLen) {
		if (numLen == 0) {
			return prefix;
		}
		StringBuffer sb = new StringBuffer(StringHelper.defaultIfBlank(prefix,""));
		Random rnd = new Random();  
	    for(int i=0; i < numLen; i++) {
	    	sb.append((char)('0' + rnd.nextInt(10)));
	    }
	    return sb.toString();  
	}
	
	/**
	 * 随机产生编码
	 * @param length 长度
	 * @return 随机字符串
	 */
	public static String getRandomString(int length) {
	    String base = "abcdefghijklmnopqrstuvwxyz0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString().toUpperCase();   
	 }   

	/**
	 * 随机产生编码
	 * @param length 长度
	 * @return 随机字符串
	 */
	public static String getRandomNumber(int length) {
	    String base = "0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString().toUpperCase();   
	 }   

	/**
	 * 随机产生编码
	 * @param length 长度
	 * @return 随机字符串
	 */
	public static String getSerialString() {
	    String base = "0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < 6; i++) {   
	        int number = random.nextInt(base.length());   
	        sb.append(number);
	    } 
	    long s = System.currentTimeMillis()*1000000+Integer.parseInt(sb.toString());
	    return DigitalFor62Helper.to62(s, 12);   
	 }  
	
	
	final public static String arrayFormat(final String messagePattern, final Object... argArray) {
        int i = 0;
        int j;
        StringBuilder sbuf = new StringBuilder(messagePattern.length() + 50);
        int L;
        for (L = 0; L < argArray.length; L++) {
            j = messagePattern.indexOf("{}", i);
            if (j != -1){
                sbuf.append(messagePattern,i,j);
                sbuf.append(argArray[L]);
                i = j+2;
            }else{
                break;
            }
        }
        sbuf.append(messagePattern, i, messagePattern.length());
        return sbuf.toString();
    }

    
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  

    	String  s  = arrayFormat("11{}22","aaa");
        System.out.println(s);   //120160524121052485
    }  
	
}
